package com.amos.muexamclient.sys.controller;

import com.amos.bean.Result;
import com.amos.muexamclient.sys.service.SysDeptService;
import com.amos.muexamclient.sys.service.dto.SysDeptDTO;
import com.amos.muexamcore.core.BasePageController;
import com.amos.muexamcore.core.BasePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamclient.sys.controller
 * @ClassName SysDeptController
 * @Description TODO
 * @Author Amos
 * @Modifier
 * @Date 2020-03-15 10:39:17
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/sys/dept")
public class SysDeptController extends BasePageController<SysDeptDTO> {
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public BasePageService<SysDeptDTO> getPageService() {
        return sysDeptService;
    }

    @PostMapping(value = "/tree-select")
    public Result treeselect(@RequestBody SysDeptDTO dto) {
        return Result.success(sysDeptService.treeSelect(dto));
    }

    /**
     * 加载对应角色部门列表树
     * @param id
     * @return
     */
    @GetMapping(value = "/role-dept-tree-select/{roleId}")
    public Result roleDeptTreeselect(@PathVariable("roleId") String id) {
        return Result.success(this.sysDeptService.roleDeptTreeselect(id));
    }

    @DeleteMapping(value = "/{deptId}")
    public Result remove(@PathVariable("deptId") String deptId) {
        if (sysDeptService.hasChildByDeptId(deptId)) {
            return Result.error("存在下级部门,不允许删除");
        }
        if (sysDeptService.checkDeptExistUser(deptId)) {
            return Result.error("部门存在用户,不允许删除");
        }
        return Result.success(this.sysDeptService.logicDeleted(deptId));
    }
}
